package com.example.mydemo.camerax

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.blankj.utilcode.util.ConvertUtils

/**
 * CreateBy:Joker
 * CreateTime:2023/7/21 17:49
 * description：
 */
class FaceOverlayView(context: Context, attrs: AttributeSet?) : View(context, attrs) {

    private val paintRect by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = Color.GREEN
            strokeWidth = ConvertUtils.dp2px(2f).toFloat()
            style = Paint.Style.STROKE
        }
    }

    private val paintText by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = Color.GREEN
            textSize = ConvertUtils.dp2px(20f).toFloat()
            style = Paint.Style.FILL
        }
    }

    private var faceResult: FaceResultEntity? = null

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        faceResult?.let {
            canvas.apply {
                save()
                drawRect(it.rect, paintRect)
                drawText("$it", it.rect.left, it.rect.bottom + paintText.textSize, paintText)
                restore()
            }
        }
    }

    fun updateFace(faceResult: FaceResultEntity?) {
        this.faceResult = faceResult
        invalidate()
    }

}